package com.suoluo.zenadmin.controllers.portal.destination;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.suoluo.zenadmin.common.UBB;
import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.service.dest.DestFeatureCategoryService;
import com.suoluo.zenadmin.service.dest.DestService;
import com.suoluo.zenadmin.service.dest.DestTrafficService;
import com.suoluo.zenadmin.vo.country.ContinentVO;
import com.suoluo.zenadmin.vo.dest.DestActivityVO;
import com.suoluo.zenadmin.vo.dest.DestAllFeatureVO;
import com.suoluo.zenadmin.vo.dest.DestFeature2VO;
import com.suoluo.zenadmin.vo.dest.DestFeatureCategoryVO;
import com.suoluo.zenadmin.vo.dest.DestFeatureItem2VO;
import com.suoluo.zenadmin.vo.dest.DestPathVO;
import com.suoluo.zenadmin.vo.dest.DestTraffic2VO;
import com.suoluo.zenadmin.vo.dest.DestVO;

@Path("")
public class DestController {

	private static final Log logger = LogFactory.getLog(DestController.class);
	
	@Resource(name="destService")
	private DestService destService;
	
	@Resource(name="categoryService")
	private DestFeatureCategoryService categoryService;
	
	@Resource(name="destTrafficService")
	private DestTrafficService destTrafficService;
	/**
	 * 目的地的首页
	 * @return
	 */
	@Get("")
	public String home(Invocation inv){
		List<ContinentVO> destMap = destService.findDestNavigateTree();
		inv.addModel("destMap", destMap);
		return "destMaps";
	}
	
	@Get("{destId:[0-9]+}")
	public String showDest(Invocation inv,@Param("destId") int destId) {
		
		//1.目的地详细信息
//		TravelDestinationPOJO dest = destService.findTravelDestinationById(destId);
		DestVO dest = destService.findDestById(destId);
		if (dest == null){
			//如果目的地不存在，则直接返回
			logger.error("dest not exist,destId=" + destId);
			//目的地不存在，重定向到目的地的首页
			return "r:" + inv.getRequestPath().getCtxpath()+"/destination";
		}
		dest.setContent(UBB.decode(dest.getContent()));
		inv.addModel("dest", dest);
		
		//2.洲信息集合
		List<ContinentVO> coninentTree = destService.findDestNavigateTree();
		inv.addModel("coninentTree", coninentTree);
		
		//4.查询活动信息,暂时屏蔽即将到来
		List<DestActivityVO> activities = Collections.emptyList();
		inv.addModel("activities",activities);
		
		if (dest.getStyle() == 1){
			//5.查询特别体验
			DestAllFeatureVO allFeatures = destService.findDestAllFeatures(destId);
			inv.addModel("sepfficExperience", allFeatures.getSpecialExperience());
			
			//6.查询特色信息
			inv.addModel("features", allFeatures.getOtherFeatures());
			inv.addModel("keyword",allFeatures.getKeyword());
			
		} else {
			//不分类展示
			List<DestFeatureItem2VO> featureItems = destService.findFeatureItemsWithoutContent(destId, 0);
			inv.addModel("featureItems", featureItems);
		}
		
		//目的地路径
		DestPathVO destPath = destService.findDestPath(destId);
		inv.addModel("destPath", destPath);
		inv.addModel("hotelCategory", SystemConfigHolder.getDestFeatureCategoryForHotel());
		return "destination";
	}
	
	@Get("{destId:[0-9]+}/{fid:[0-9]+}")
	public String showDestFeature(Invocation inv,@Param("destId") int destId,
			@Param("fid") int featureId){
		
		//1.洲信息集合
		List<ContinentVO> coninentTree = destService.findDestNavigateTree();
		inv.addModel("coninentTree", coninentTree);
		//2.目的地详细信息
		DestVO dest = destService.findSimplestDestById(destId);
		inv.addModel("dest", dest);
		
		//3.目的地路径
		DestPathVO destPath = destService.findDestPath(destId);
		inv.addModel("destPath", destPath);
		
//		DestTrafficVO trafficVO = destService.findSimpleDestTrafficByDestId(destId);
//		inv.addModel("trafficVO", trafficVO);
		
		//4.查询活动信息
		List<DestActivityVO> activities = Collections.emptyList();
		inv.addModel("activities",activities);
		
//		${cpath}/views/dest/destination.do?action=viewDestFeature&featureId=${feature.featureId}&featureCategoryId=${feature.featureCategoryId}&destId=${dest.destId}
		
		
		
		DestFeature2VO feature =  destService.findDestFeatureById(featureId);
		feature.setContent(UBB.decode(feature.getContent()));
		inv.addModel("feature",feature);
		
//		Integer featureCategoryId = feature.getFeatureCategoryId();
//		DestFeatureCategoryPOJO featureCategory = destService.findDestFeatureCategoryById(featureCategoryId);
//		inv.addModel("featureCategory",featureCategory);
		
		List<DestFeatureItem2VO> featureItems = destService.findFeatureItemsWithoutContentByFeatureId(featureId);
		if (feature.getFeatureCategoryId() == SystemConfigHolder.getDestFeatureCategoryForHotel()){
//			List<HotelPOJO> hotels = destService.findHotelFeatureItems(featureId);
			inv.addModel("hotels", featureItems);
			return "destFeatureForHotel";
		}
		else{
//			List<DestFeatureItem2VO> featureItems = destService.findFeatureItemsWithoutContentByFeatureId(featureId);
			inv.addModel("featureItems", featureItems);
//			mv.setViewName("views/dest/destFeature");
			return "destFeature";
		}
	}
	
	@Get("{destId:[0-9]+}/item/{fiid:[0-9]+}")
	public String showDestFeatureItem(Invocation inv,@Param("destId") int destId,
			@Param("fiid") int featureItemId){

		DestVO dest = destService.findSimplestDestById(destId);
		inv.addModel("dest", dest);
		
		DestFeatureItem2VO featureItem = destService.findCompleteFeatureItemById(featureItemId);
		
		if (featureItem == null){
			logger.error("feature item not exsits,[destName="+dest.getZhname()+",featureItemId="+featureItemId+"}");
			return "r:" + inv.getRequestPath().getCtxpath()+"/destination/"+destId;
		}
		inv.addModel("featureItem", featureItem);
		
		//查询特色项所在的类别
		DestFeatureCategoryVO featureCategory =categoryService.findDestFeatureCategoryById(featureItem.getFeatureCategoryId());
		inv.addModel("featureCategory",featureCategory);
		if (featureCategory.getFeatureCategoryId() == SystemConfigHolder.getDestFeatureCategorySpecial()){
			List<DestFeatureItem2VO> samilarFeatureItems = destService.findSamilarDestFeatureItems(featureItemId, featureItem.getFeatureId(), 3);
			inv.addModel("samilarFeatureItems", samilarFeatureItems);
			return "destExperience";
		} else {
			List<DestFeatureItem2VO> samilarFeatureItems = destService.findSamilarDestFeatureItems(featureItemId, featureItem.getFeatureId(), 4);
			inv.addModel("samilarFeatureItems", samilarFeatureItems);
			return "destFeatureItem2";
		}
	} 
	
	@Get("{destId:[0-9]+}/traffic")
	public String showDestTraffic(Invocation inv,@Param("destId") int destId){
		
		//1.洲信息集合
		List<ContinentVO> coninentTree = destService.findDestNavigateTree();
		inv.addModel("coninentTree", coninentTree);
		//2.目的地详细信息
//		TravelDestinationPOJO dest = destService.findTravelDestinationById(destId);
		DestVO dest = destService.findSimplestDestById(destId);
		inv.addModel("dest", dest);
		
		//3.目的地路径
		DestPathVO destPath = destService.findDestPath(destId);
		inv.addModel("destPath", destPath);
		
//		DestTrafficVO trafficVO = destService.findSimpleDestTrafficByDestId(destId);
//		inv.addModel("trafficVO", trafficVO);
		
		//4.查询活动信息
		List<DestActivityVO> activities = Collections.emptyList();
		inv.addModel("activities",activities);
		
		//3.查询交通信息
		List<DestTraffic2VO> list = destTrafficService.findAllDestTraffic(destId);
		inv.addModel("traffics", list);
		
		return "listDestTraffic";
	} 
}
